<?php

namespace Fronts\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use System\Model\Catalog;
use Zend\Session\Container;

class CatalogTable extends AbstractTableGateway {

    protected $table = "tblcatalog";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Catalog());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function loadcatalog($parentId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        if ($parentId > -1) {
            $sqlEx->where(array("catalogParent" => $parentId));
        }
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $result) {
            $data[] = $result;
        }
        return $data;
    }

    public function loadcatalogindex() {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array(
            "catalogId" => "catalogId",
            "catalogName" => "catalogName",
            "catalogLink" => "catalogLink",
        ));
        $sqlEx->where(array(
            "catalogActive" => "1",
            "showindex" => "1",
        ));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = array();
        foreach ($resultSet as $list) {
            $data[] = $list;
        }
        return $data;
    }

    public function getnamecatalog($catalogLink) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogName" => "catalogName", "catalogId" => "catalogId"));
        $sqlEx->where(array("catalogLink" => $catalogLink));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $data = $rs->current();
        return $data;
    }
    
    public function scanlistparentcata($rootmenu) {
        $index = 1;

        $listcataId = array();
        $listcataId[0] = $rootmenu;
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogId" => "catalogId"));
        $sqlEx->where(array("catalogParent" => $rootmenu));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $results) {
            $data[] = $results;
        }
        $arr = array();
        $index2 = 0;
        $def = "";
        foreach ($data as $key => $scan) {
            array_push($listcataId, $scan["catalogId"]);
            $strr = $this->scacsubmenu($index + 1, $scan["catalogId"], $listcataId, $def);
            $arr[$index2] = $strr;
            $index2++;
        }
        $hello = implode("-", $arr);
        return $hello;
    }

    public function scacsubmenu($index, $cataID, $listcataId, $arr) {

        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogId" => "catalogId"));
        $sqlEx->where(array("catalogParent" => $cataID));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $results) {
            $data[] = $results;
        }
        foreach ($data as $key => $scan) {
            array_push($listcataId, $scan["catalogId"]);
            $arr.=$this->scacsubmenu($index + 1, $scan["catalogId"], $listcataId, $arr) . "-";
        }


        $arr .= implode("-", $listcataId);
        return $arr;
    }
    

   

}
